package com.bawei.myclub.base.mvvm.view

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import kotlinx.coroutines.flow.SharedFlow


/**
 *作者：lxy
 *功能:
 *时间：2025/5/12
 */

abstract class BaseFragment<VM : BaseViewModel, VDB : ViewDataBinding> : Fragment() {

    protected abstract val mLayoutResId: Int
    protected lateinit var mBinding: VDB
    protected abstract val mViewModel: VM
    protected lateinit var mParentView: View
    open val mPageCanBack: Boolean = true

    val mCanBack: Boolean
        get() {
            if (mPageCanBack) {
                return mPageCanBack
            }
            val fragments = childFragmentManager.fragments
            if (fragments.isEmpty()) {
                return mPageCanBack
            } else {
                for (fragment in fragments) {
                    if (fragment is BaseFragment<*, *>) {
                        Log.d(
                            "pressBack",
                            "child fragment :${fragment} -- ${fragment.isVisible} -- ${fragment.mPageCanBack}"
                        )
                        if (fragment.isVisible) {
                            return fragment.mPageCanBack
                        }
                    }
                }
            }
            return mPageCanBack
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = DataBindingUtil.inflate(layoutInflater, mLayoutResId, container, false)
        mBinding.lifecycleOwner = viewLifecycleOwner
        mParentView = mBinding.root
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initListener()
        initData()
    }


    open fun initView() {}

    open fun initData() {}

    open fun initListener() {}


    /**
     * 隐藏键盘
     */
    fun hideSoftKeyBroad(view: View) {
        if (requireActivity() is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).hideKeyboard(view)
        }
    }

    /**
     * 显示键盘
     */
    fun showSoftKeyBroad(view: View) {
        if (activity is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).showSoftKeyBoard(view)
        }
    }

    open fun setLoadingViewVisible(show: Boolean, msg: String = "") {
        if (activity is BaseActivity<*, *>) {
            (requireActivity() as BaseActivity<*, *>).setLoadingViewVisible(show, msg)
        }
    }

    /**
     * 通用状态监听：Loading显示和隐藏、错误提示
     */
    open fun addCommonStateListener(stateListener: SharedFlow<RequestEvent>) {
        stateListener.observeWithLifecycle(this, Lifecycle.State.STARTED) {
            when (it) {
                is Success -> {
                    setLoadingViewVisible(false)
                }

                is Loading -> {
                    setLoadingViewVisible(true)
                }

                is Failed -> {
                    setLoadingViewVisible(false)
                    Toast.makeText(requireContext(), it.message, Toast.LENGTH_SHORT)
                }

                is Empty -> {
                    setLoadingViewVisible(false)
                    (requireActivity() as BaseActivity<*,*>).showToast("数据为空")
                }
            }
        }
    }


}